<?php
namespace Core\Forms\Element;

use Phalcon\Di;

class Autoinput extends Text
{
    public function render($attributes = null)
    {
        $di = Di::getDefault();

        $source = isset($this->_options['source']) ? $this->_options['source'] : $di->getShared('url')->get(['for'=>'autoinput','id'=>$this->_options['sourceId']]);
        if (isset($this->_options['script'])) {
            $script = $this->_options['script'];
        } else {
            $script =
<<<scription
$("#{$this->_name}").easyAutocomplete({
  url: "{$source}"
});
scription;
        }

        $di->getShared('assets')
            ->addJs('easy-autocomplete', '//cdn.bootcss.com/easy-autocomplete/1.3.5/jquery.easy-autocomplete.min.js', 'footer')
            ->addCss('easy-autocomplete', '//cdn.bootcss.com/easy-autocomplete/1.3.5/easy-autocomplete.min.css', 'footer')
            ->addCss('easy-autocomplete-theme', '//cdn.bootcss.com/easy-autocomplete/1.3.5/easy-autocomplete.themes.min.css', 'footer')
            ->addInlineJs('easy-autocomplete-init-' . $this->_name, $script, 'footer');
        return parent::render($attributes);
    }
}
